﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

using SmlEngine;
using SmlEngine.Sprites.Collections;
using SmlEngine.Sprites.Base.WorldMap;

namespace SmlEngine.Sprites.Base.WorldMap
{
    /// <summary>
    /// A tile that is used to transport WorldPlayers
    /// from across worlds or to other worlds.
    /// </summary>
    public sealed class PipeTile : PathTile, ICloneable, IWorldDrawable
    {
        /// <summary>
        /// The display name of this tile, displays on the world border.
        /// </summary>
        public string DisplayName
        {
            get;
            set;
        }

        /// <summary>
        /// Which section this tile leads to.
        /// </summary>
        public int DestinationWorldSection
        {
            get;
            set;
        }

        /// <summary>
        /// Where on the world this tile leads to.
        /// </summary>
        public Vector2 DestinationPosition
        {
            get;
            set;
        }

        /// <summary>
        /// The destination position measured in tiles.
        /// </summary>
        public Vector2 DestinationTileLocation
        {
            get
            {
                if (DestinationPosition != null && base.worldTile != null)
                {
                    Vector2 size = base.worldTile.Size;
                    Vector2 dest = this.DestinationPosition;
                    return new Vector2((float)Math.Floor(dest.X / size.X), (float)Math.Floor(dest.Y / size.Y));
                }
                else return new Vector2(float.NaN, float.NaN);
            }
        }

        public PipeTile(ContentManager Content, GraphicsDevice Graphics, string graphicsSource, Rectangle sourceRect)
            : base(Content, Graphics, graphicsSource, new Vector2(sourceRect.X, sourceRect.Y), new Vector2(sourceRect.Width, sourceRect.Height),
            PathType.FourWayIntersection, false)
        {
            base.worldTile.Start();
        }

        public PipeTile(ContentManager Content, GraphicsDevice Graphics, string graphicsSource, List<Rectangle> sourceRects)
        {
            base.worldTile = new WorldTile(graphicsSource, sourceRects);
            base.IsAnimated = true;
            base.Direction = PathType.FourWayIntersection;
            base.IsNode = false;
            base.worldTile.Start();
        }

        public void LoadContent(GraphicsDevice gd, ContentManager cm)
        {
            base.LoadContent(cm, gd);
        }

        public void UnloadContent()
        {
            base.UnloadContent();
        }

        public void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            base.Draw(gameTime, spriteBatch);
        }
    }
}
